<template>
    <div class="edit-dialog-container">
        <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px" destroy-on-close>
            <el-form ref="userDialogFormRef" :model="state.content" :rules="formRules" size="default" label-width="90px">
                <el-row :gutter="35">
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item prop="code" label="序列号">
                            <el-input v-model="state.content.code" :disabled="true" placeholder="请输入序列号"
                                clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item prop="name" label="门名称">
                            <el-input v-model="state.content.name" placeholder="请输入设备别名" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="进出方向">
                            <el-select v-model="state.content.inout" class="m-2" placeholder="请选择" size="default" clearable>
                                <el-option label="进" :value="1" />
                                <el-option label="出" :value="2" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="是否登记机">
                            <el-select v-model="state.content.isAdmin" class="m-2" placeholder="请选择" size="default"
                                clearable>
                                <el-option label="是" :value="1" />
                                <el-option label="否" :value="2" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
                        <el-form-item label="关联区域">
                            <el-cascader :options="state.areaTreeData"
                                :props="{ multiple: false, checkStrictly: true, value: 'code', label: 'name' }"
                                placeholder="请选择区域" clearable class="w100" v-model="state.areaCode">
                                <template #default="{ node, data }">
                                    <span>{{ data.name }}</span>
                                    <span v-if="!node.isLeaf"> ({{ data.children.length }}) </span>
                                </template>
                            </el-cascader>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>

            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="onSubmitContinue" type="primary" size="default" v-if="state.dialog.type === 0">{{
                        t('message.btnName.continueSubmit')
                    }}</el-button>
                    <el-button @click="onSubmit" type="success" size="default">{{ t('message.btnName.submit')
                    }}</el-button>
                    <el-button @click="onCancel" size="default">{{ t('message.btnName.cancel') }}</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts" >
import { reactive, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { ElMessage, UploadProps } from 'element-plus';
import { fileToBase64 } from '/@/utils/files';
import { usePersonOrgTreeApi } from '/@/api/person/org';
import { usePersonEmployeeQueryApi, usePersonEmployeeUpdateApi, usePersonEmployeeCreateApi } from '/@/api/person/employee';
import { usePersonRoleListApi } from '/@/api/person/role';
import { useAccessPointCreateApi, useAccessPointQueryApi, useAccessPointUpdateApi } from '/@/api/access/point';
import { useAccessAreaTreeApi } from '/@/api/access/area';

// 定义子组件向父组件传值/事件
const { t } = useI18n();
const emit = defineEmits(['refresh']);

// 定义变量内容
const orgList = ref<any[]>([])
const roleList = ref<any[]>([])

const useContent = () => {
    return {
        code: '', // 账户名称
        name: '', // 用户姓名
        remark: '', // 邮箱
        password: '', // 账户密码
        newPassword: '',
        privilege: 0,
        avatar: '',
        redirect: '',
    }
}

const state = reactive({
    content: {} as any,
    areaCode: '',
    areaTreeData: [],
    dialog: {
        isShowDialog: false,
        type: 0,
        title: '',
    },
});

// 表单校验
const formRules = ref({
    code: [
        { required: true, message: '必填项不能为空', trigger: 'blur' },
        { min: 1, max: 40, message: '工号必须1-40位字母数字组合而成', trigger: 'blur' },
        // { pattern: /^[a-z1-9]/, message: '工号必须非0数字或小写字母开头', trigger: 'blur' },
    ],
    name: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
    orgCode: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
    roleCode: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
    mobile: [{ pattern: /^0{0,1}(13[0-9]|15[7-9]|153|156|18[7-9])[0-9]{8}$/, message: '请输入正确手机号', trigger: 'blur' },],
})

// 打开弹窗
const openDialog = async (code?: string) => {
    state.dialog.isShowDialog = true
    areaData()
    if (code) {
        state.dialog.title = t('message.btnName.edit')
        state.dialog.type = 1
        queryItem(code)
    } else {
        state.dialog.title = t('message.btnName.addRow')
        state.dialog.type = 0
        state.content = useContent()
    }
};
// 关闭弹窗
const closeDialog = () => {
    state.dialog.isShowDialog = false;
    state.content = useContent()
    emit('refresh');
};
// 取消
const onCancel = () => {
    closeDialog();
};
const queryItem = (code: string) => {
    useAccessPointQueryApi(code).then(({ content }) => {
        state.content = content
    })
}

const areaData = () => {
    useAccessAreaTreeApi('').then(({ content }) => {
        state.areaTreeData = content
    })
}
// 提交
const onSubmit = () => {
    if (state.dialog.type === 1) {
        useAccessPointUpdateApi(Object.assign(state.content, { areaCode: state.areaCode[state.areaCode.length - 1] })).then(({ content }) => {
            console.log(content);
        }).then(() => {
            ElMessage({
                type: 'success',
                message: '操作成功'
            })
            closeDialog()
        })
    } else {
        useAccessPointCreateApi(state.content).then(({ content }) => {
            console.log(content);
        }).then(() => {
            ElMessage({
                type: 'success',
                message: '操作成功'
            })
            closeDialog()
        })
    }
};
// 提交
const onSubmitContinue = () => {
    useAccessPointCreateApi(state.content).then(() => {
        ElMessage({
            type: 'success',
            message: '操作成功'
        })
    }).then(() => {
        state.content = useContent()
    })
};



defineExpose({
    openDialog,
});
</script>


<style scoped lang="scss">
:deep(.el-dialog__header) {
    margin: 0;
    border-bottom: 1px dashed rgba(0, 0, 0, .2);
}

:deep(.el-dialog__footer) {
    border-top: 1px dashed rgba(0, 0, 0, .2);
}
</style>